package com.shunda.pay.utils;

import com.shunda.common.exception.ServiceException;
import com.shunda.pay.config.WxPayConfig;
import com.shunda.pay.dto.WxTransferDto;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferRequest;
import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferResponse;
import com.wechat.pay.java.service.transferbatch.model.TransferDetailInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 转账到零钱工具类
 */
public class WxTransferUtil {

    private static final Logger log = LoggerFactory.getLogger(WxTransferUtil.class);

    /**
     * 封装微信支付申请退款请求参数
     *
     * @param dto 微信支付申请退款请求参数
     * @return 封装后的map微信支付申请退款请求参数对象
     */
    private static InitiateBatchTransferRequest getTransferParams(WxPayConfig wxPayConfig, WxTransferDto dto) {
        InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
        request.setAppid(wxPayConfig.getAppId());
        request.setOutBatchNo(dto.getOutBillNo());
        request.setBatchName("【" + dto.getUserName() + "】提现");
        request.setBatchRemark("【" + dto.getUserName() + "】提现成功");
        request.setTotalAmount(dto.getAmount());
        request.setTotalNum(1);
        List<TransferDetailInput> transferDetailList = new ArrayList<>();
        TransferDetailInput transferDetailInput = new TransferDetailInput();
        transferDetailInput.setOutDetailNo(dto.getOutBillNo());
        transferDetailInput.setOpenid(dto.getOpenId());
        transferDetailInput.setUserName(dto.getUserName());
        transferDetailInput.setTransferAmount(dto.getAmount());
        transferDetailInput.setTransferRemark("提现");
        transferDetailList.add(transferDetailInput);
        request.setTransferDetailList(transferDetailList);
        // 回调通知地址
        request.setNotifyUrl(wxPayConfig.getTransferNotifyUrl());
        return request;
    }

    /**
     * 转账到零钱
     *
     * @param wxPayConfig 微信配置信息
     * @param param       微信支付申请退款请求参数
     * @return 微信支付二维码地址
     */
    public static InitiateBatchTransferResponse transferToWallet(WxPayConfig wxPayConfig, WxTransferDto param,
                                                                 RSAAutoCertificateConfig rsaAutoCertificateConfig) {
        TransferBatchService service =
                new TransferBatchService.Builder()
                        .config(rsaAutoCertificateConfig)
                        .build();
        InitiateBatchTransferResponse response = null;
        try {
            InitiateBatchTransferRequest request = getTransferParams(wxPayConfig, param);
            response = service.initiateBatchTransfer(request);
        } catch (HttpException e) { // 发送HTTP请求失败
            log.error("微信请求提现发送HTTP请求失败，错误信息：{}", e.getHttpRequest());
        } catch (ServiceException e) { // 服务返回状态小于200或大于等于300，例如500
            log.error("微信提现服务状态错误，错误信息：{}", e.getMessage());
        } catch (MalformedMessageException e) { // 服务返回成功，返回体类型不合法，或者解析返回体失败
            log.error("服务返回成功，返回体类型不合法，或者解析返回体失败，错误信息：{}", e.getMessage());
        }
        if (response != null) {
            log.info("微信提现成功，返回值：{}", response);
            // 返回值
            return response;
        }
        return null;
    }
}
